<?php
// 登录
class UsersAction extends Action {
	
	private $cLogs = null;
	private $cUsers = null;
	private $cPlantform = null;
	private $cAdmin = null;
	
	function __construct(){
		
		//判断管理员登录状态
		admin_login_check();
					
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.Users');
		import('@.ORG.Plantform');
		import('@.ORG.Logs');
		import('@.ORG.Admin');
		
		$this->cUsers 		= new Users();
		$this->cPlantform 	= new Plantform();
		$this->cLogs 		= new Logs();
		$this->cAdmin 		= new Admin();
	}
	
	public function index(){

		admin_role('Users:index');

		$search 	= $this->_get('search'		,'','');
    	$keywords 	= $this->_get('keywords'	,'','');
    	$page   	= $this->_get('p'			,'','1');
    	$pageSize 	= 100;
    		
    	    	
    	$search_items = array(
	    	'shop_name'				=>'u.shop_name',	    	
    		'admin_real_name'		=>'a.real_name',
    		'plantform_name'		=>'pf.plantform_name'
    	);
    	
    	
    	$where = array();
    	if(array_key_exists($search, $search_items)){
    		$where = array(
    			$search_items[$search] => $keywords
    		);
    	}
    	    	
    	$allCount = 0;
    	
    	$users = $this->cUsers->lists($where,$page,$pageSize,$allCount);
    	
    	//查询条件 
    	$user_search = C('USER_SEARCH');
    	
    	/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
		//导入分页类
		import("@.ORG.Page");
		
		$pageParams = array(
			'g'		=> 'Admin',
			'begin' => $begin,
			'end'	=> $end,
			'search' => $search,
			'keywords' => $keywords
		);
		 // 实例化分页类
	  	$p = new Page($allCount,$pageSize,$pageParams);
	  	
    	// 分页显示输出
    	$pager = $p->show();

		/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'users'		=> $users,
    			'search'	=> $search,
    			'keywords'	=> $keywords,
    			'pager'		=> $pager,
    			'user_search'	=> $user_search,
    			'MainPage'	=> 'Users:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public  function detail(){
    	
    	admin_role('Users:edit');
    			
    	$get = get(array(
    	  	array('id'			,'intval',0)
    	));
		
    	$id = $get['id'];
    	
    	$user = $this->cUsers->get_by_user_id($id);
     	   	
    	$plantforms = $this->cPlantform->lists();
    	 
    	//管理员
    	$admins = $this->cAdmin->lists();
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'user'		=> $user,
    			'plantforms'=> $plantforms,
    			'admins'	=> $admins,
    			'MainPage'	=> 'Users:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    	
    }
    
    public function add(){

    	admin_role('Users:add');
    	
    	$plantforms = $this->cPlantform->lists();
    	
    	//管理员
    	$admins = $this->cAdmin->lists();
    	
    	$shop_created = short_date();
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'plantforms'=> $plantforms,
    			'admins'	=> $admins,
    			'shop_created'=>$shop_created,
    			'MainPage'	=> 'Users:add'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){

    	admin_role('Users:add');
    	
    	$post = post(array(
    	  	array('user_name'			,'trim',''),
    		array('user_passwd'			,'trim',''),
    		array('user_email'			,'trim',''),
    		array('qq'					,'trim',''),
    	  	array('mobile_phone'		,'trim',''),
    		array('real_name'			,'trim',''),
    		array('location'			,'trim',''),
    		array('disabled'			,'trim',''),
    	  	array('plantform_id'		,'intval',0),
    	  	array('business_admin_id'	,'intval',0),
    	  	array('category'			,'trim',''),
    		array('shop_name'			,'trim',''),
    		array('shop_url'			,'trim',''),
    		array('shop_created'		,'trim','')
    	));
    
    	$add_result = $this->cUsers->register($post);
    	
    	if($add_result){
    		
    		$this->cLogs->add('Users:add','添加客户成功:'.$post['shop_name']);
    		
    		json_success('客户添加成功');
    	}else{
    		json_error('客户添加失败');
    	}
    }
    
 	public function update(){
    	
 		admin_role('Users:edit');
 				
      	$post = post(array(
    		array('user_id'				,'intval',''),
    		array('user_email'			,'trim',''),
    		array('qq'					,'trim',''),
    	  	array('mobile_phone'		,'trim',''),
    		array('real_name'			,'trim',''),
    		array('location'			,'trim',''),
    		array('disabled'			,'trim',''),
    	  	array('plantform_id'		,'intval',0),
    	  	array('business_admin_id'	,'intval',0),
    	  	array('category'			,'trim',''),
    		array('shop_name'			,'trim',''),
    		array('shop_url'			,'trim',''),
    		array('shop_created'		,'trim','')
    	));
    	
    	$id = $post['user_id'];
   
    	$update_result = $this->cUsers->update($id,$post);
    	
 	    if($update_result){
 	    	
 	    	$this->cLogs->add('Users:edit','客户更新成功:'.$post['shop_name']);
 	    	
    		json_success('保存成功');
    	}else{
    		json_error('保存失败');
    	}
    }
}
?>